<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
    <script src="__CDN__/assets/libs/fastadmin-layer/dist/layer.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>绑定景珍账号</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f3f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .container {
            background: #ffffff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 90%;
            max-width: 400px;
            text-align: center;
        }
        h2 {
            margin-bottom: 20px;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #666;
        }
        .form-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
            font-size: 16px;
        }
        .form-group button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }
        .form-group button:hover {
            background-color: #0056b3;
        }
        .error-message {
            color: red;
            font-size: 14px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>绑定账号</h2>
    <div class="form-group">
        <label for="phone">手机号</label>
        <input type="tel" id="phone" placeholder="请输入手机号" maxlength="11">
        <div class="error-message" id="phone-error"></div>
        <input type="hidden" id="openid" value="{$openid}">
    </div>
    <div class="form-group">
        <button onclick="bindAccount()" id="bind-btn" disabled>绑定</button>
    </div>
</div>

<script>
    document.getElementById('phone').addEventListener('input', validateForm);

    function validateForm() {
        const phoneInput = document.getElementById('phone');
        const bindBtn = document.getElementById('bind-btn');
        const phoneError = document.getElementById('phone-error');

        const phone = phoneInput.value.trim();

        if (!/^1[3456789]\d{9}$/.test(phone)) {
            phoneError.textContent = '请输入有效的11位手机号';
            bindBtn.disabled = true;
        } else {
            phoneError.textContent = '';
            bindBtn.disabled = false;
        }
    }

    function bindAccount() {
        const phoneInput = document.getElementById('phone');
        const openid = document.getElementById('openid').value;
        const phone = phoneInput.value.trim();

        // 这里可以添加发送请求到服务器进行实际绑定操作的代码
        $.ajax({
            type: 'POST',
            url: '/api/Weixin/bind',
            dataType: "json",
            data: {
                phone:phone,
                openid:openid,
            },
            success: function (ret) {
                if(ret.code==1){
                    layer.open({
                        type: 1,
                        area: ['280px', '180px'],
                        content: '<p style="padding: 15px;font-size: 16px;color: #ff0000;">'+ret.msg+'</p>',
                        btn: ['确定'], // 弹窗按钮
                        btnAlign:"c",
                        yes: function(index, layero){
                            // 点击确定按钮后的回调函数
                            // index是弹窗的索引，layero是弹窗的DOM对象
                            layer.close(index); // 关闭弹窗
                            window.close();

                        }
                    });
                }else {
                    layer.open({
                        type: 1,
                        area: ['280px', '180px'],
                        content: '<p style="padding: 15px;font-size: 16px;color: #ff0000;">'+ret.msg+'</p>',
                        btn: ['确定'], // 弹窗按钮
                        btnAlign:"c",
                        yes: function(index, layero){
                            // 点击确定按钮后的回调函数
                            // index是弹窗的索引，layero是弹窗的DOM对象
                            layer.close(index); // 关闭弹窗
                            window.location='/api/Weixin/bind';
                        }
                    });
                }
            },
            error: function (data) {
                layer.open({
                    type: 1,
                    area: ['280px', '180px'],
                    content: '<p style="padding: 15px;font-size: 16px;color: #ff0000;">'+data.msg+'</p>',
                    btn: ['确定'], // 弹窗按钮
                    btnAlign:"c",
                    yes: function(index, layero){
                        // 点击确定按钮后的回调函数
                        // index是弹窗的索引，layero是弹窗的DOM对象
                        layer.close(index); // 关闭弹窗
                    }
                });
            }
        });
        return false;
    }
</script>
</body>
</html>